import japa.parser.*;
import japa.parser.ast.CompilationUnit;
import japa.parser.ast.body.MethodDeclaration;
import japa.parser.ast.body.ModifierSet;
import japa.parser.ast.visitor.VoidVisitorAdapter;
import java.io.File;
import java.io.FileInputStream;
import javax.swing.JFileChooser;
import javax.swing.JFrame;

public class NumberPublicMethods {

	/**
	 * Counts the number of public methods, not including the constructor or class signature
	 */
	
	public static void main(String[] args) throws Exception {

		final JFileChooser fc = new JFileChooser();

		fc.showOpenDialog(new JFrame());

		// creates an input stream for the file to be parsed
		File file = fc.getSelectedFile();
		System.out.println(file.getName());

		FileInputStream in = new FileInputStream(file.getAbsolutePath());

		CompilationUnit cu;
		try {
			// parse the file
			cu = JavaParser.parse(in);
		} finally {
			in.close();
		}

		// visit and print the methods names
		NPMVisitor mv = new NPMVisitor();
		mv.visit(cu, null);
		mv.printCount();

	}

	/**
	 * Simple visitor implementation for visiting MethodDeclaration nodes.
	 */
	private static class NPMVisitor extends VoidVisitorAdapter {
		int count = 0;

		
		@Override
		public void visit(MethodDeclaration n, Object arg) {
			// here you can access the attributes of the method.
			// this method will be called for all methods in this
			// CompilationUnit, including inner class methods

			
			
			if (ModifierSet.isPublic(n.getModifiers())) {
				count++;
				System.out.println('\t' + n.getName());
			}

		}

		public void printCount() {
			System.out.println("Total Public Method Count " + count);
		}

	}
}